package com.drug.mingkang.controller;
import com.drug.mingkang.mapper.HqDatadicMapper;
import com.drug.mingkang.model.HqDatadic;
import com.drug.mingkang.model.HqRawmaterialType;
import com.drug.mingkang.service.HqDatadicService;
import com.drug.mingkang.utils.ResponseMessage;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 数据字典(HqDatadic)表控制层
 *  陆林
 * @author wenchangjian
 * @since 2020-08-11 12:00:12
 */
@RestController
@RequestMapping("hqDatadic")
public class HqDatadicController {
    /**
     * 服务对象
     */
    @Resource
    private HqDatadicService hqDatadicService;

    @Resource
    private HqDatadicMapper  mapper;


    ResponseMessage  bootmsg = new ResponseMessage();
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public HqDatadic selectOne(String id) {
        return this.hqDatadicService.queryById(id);
    }


    /**
     *  显示所有数据
     */
    @PostMapping("selectAll")
    public Map<String,Object> selectA(HqDatadic  datadic){
        Map<String, Object> map = new HashMap<>();
        List<HqDatadic> list = hqDatadicService.queryAllByLimit(datadic);
        map.put("code",0);
        map.put("count", mapper.count());
        map.put("data", list);
        map.put("msg", "欢迎进来页面");
        return map;
    }

    @PostMapping("deleteById")
    public ResponseMessage delete(String id){
        int rows = hqDatadicService.deleteById(id);
        if (rows>0){
            bootmsg.setStatus(1);
            bootmsg.setMessage("删除成功");
        }else{
            bootmsg.setMessage("删除失败");
            bootmsg.setStatus(0);
        }
        return bootmsg;
    }

    @PostMapping("insertOn")
    public ResponseMessage insert(HqDatadic datadic){
        int rows = 0 ;
        if (datadic.getDicId()!=null && !datadic.getDicId().equals("")){
             rows = hqDatadicService.update(datadic);
            System.out.println("修改");
        }else {
             datadic.setSort(1);
             datadic.setDicId(UUID.randomUUID().toString());
             rows = hqDatadicService.insert(datadic);
             System.out.println("增加");
        }
        if (rows>0){
            bootmsg.setStatus(1);
            bootmsg.setMessage("操作成功");
        }else {
            bootmsg.setMessage("操作失败");
            bootmsg.setStatus(0);
        }
        return bootmsg;
    }


    /**
     * 查询所有类型
     * @param hqDatadic
     * @return
     */
    @GetMapping("selectAt")
    public List<HqDatadic> selectTypeName(HqDatadic hqDatadic){
        List<HqDatadic> list = hqDatadicService.queryAll(hqDatadic);
        return list ;
    }

}